package com.syezon.wifikey.ad;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.blankj.utilcode.util.AppUtils;
import com.hodanet.appadvplatclient.ReportAgent;
import com.hodanet.appadvplatclient.ad.AdInfo;
import com.syezon.wifikey.analysis.AnalysisHelper;
import com.syezon.wifikey.channel.ChannelConfig;
import com.syezon.wifikey.download.DownloadHelper;
import com.syezon.wifikey.news.YiYouLiaoNewsTabActivity;
import com.syezon.wifikey.web.WebHelper;

public class AdUtils {

    public static void adClicked(final Context context, final AdInfo adInfo, AdPositionEnum adPositionEnum) {
        try {
            if (adInfo.getType() == AdInfo.AD_TYPE_APK) {
                DownloadHelper.downloadUrl(adInfo, AdPositionEnum.SPLASH);
            } else if (adInfo.getType() == AdInfo.AD_TYPE_WANG_YI) {
//                Stats.event(context, "wangyinews_show");
                Intent intent = new Intent(context, YiYouLiaoNewsTabActivity.class);
                context.startActivity(intent);
            } else if (adInfo.getType() == AdInfo.AD_TYPE_DEEPLINK) {
                Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(adInfo.getUrl()));
                if (in.resolveActivity(context.getPackageManager()) == null) {
                    WebHelper.showAd(context, adInfo.getName(), adInfo.getUrl(), new WebHelper.SimpleWebLoadCallBack() {
                        @Override
                        public void loadComplete(String url) {
                            super.loadComplete(url);
                            ReportAgent.reportAdvShow(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
                        }
                    });
                } else {
                    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    context.startActivity(in);
                }
            }else {
                WebHelper.showAd(context, adInfo.getName(), adInfo.getUrl(), new WebHelper.SimpleWebLoadCallBack() {
                    @Override
                    public void loadComplete(String url) {
                        super.loadComplete(url);
                        ReportAgent.reportAdvShow(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
                    }
                });
            }
            ReportAgent.reportAdvClick(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
            AnalysisHelper.onAdEvent(adPositionEnum, AnalysisHelper.EVENT_CLICK, adInfo.getId() + "");
        } catch (Exception e) {

        }
    }

    public static void adClicked(final Context context, final AdInfo adInfo) {
        try {
            if (adInfo.getType() == AdInfo.AD_TYPE_APK) {
                DownloadHelper.downloadUrl(adInfo, AdPositionEnum.SPLASH);
            } else if (adInfo.getType() == AdInfo.AD_TYPE_DEEPLINK) {
                Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(adInfo.getUrl()));
                if (in.resolveActivity(context.getPackageManager()) == null) {
                    //说明系统中不存在这个activity
                    String[] split = adInfo.getUrl().split("://");
                    //说明系统中不存在这个activity
                    WebHelper.showAd(context, adInfo.getName(),"https://" + split[1], new WebHelper.SimpleWebLoadCallBack() {
                        @Override
                        public void loadComplete(String url) {
                            super.loadComplete(url);
                            ReportAgent.reportAdvExtShow(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
                        }
                    });
                } else {
                    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    context.startActivity(in);
                    //如果想要加载成功跳转可以 这样
                }
                //ReportAgent.reportAdvClick(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
            } else if (adInfo.getType() == AdInfo.AD_TYPE_WANG_YI) {
//                Stats.event(context, "wangyinews_show");
                Intent intent = new Intent(context, YiYouLiaoNewsTabActivity.class);
                context.startActivity(intent);
            } else {
                WebHelper.showAd(context, adInfo.getName(),adInfo.getUrl(), new WebHelper.SimpleWebLoadCallBack() {
                    @Override
                    public void loadComplete(String url) {
                        super.loadComplete(url);
                        ReportAgent.reportAdvExtShow(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
                    }
                });
            }
            ReportAgent.reportAdvClick(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
            AnalysisHelper.onAdEvent(AdPositionEnum.SPLASH, AnalysisHelper.EVENT_CLICK, adInfo.getId() + "");
        } catch (Exception ignored) {

        }
    }
}
